package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.domain.vo.CategorieVo;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TDish;
import com.ruoyi.system.service.ITDishService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 菜品Controller
 *
 * @author ruoyi
 * @date 2025-06-20
 */
@RestController
@RequestMapping("/system/dish")
public class TDishController extends BaseController
{
    @Autowired
    private ITDishService tDishService;
    @Autowired
    private ISysDictTypeService dictTypeService;
    /**
     * 查询菜品列表
     */
    @PreAuthorize("@ss.hasPermi('system:dish:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDish tDish){
        startPage();
        List<TDish> list = tDishService.selectTDishList(tDish);
        return getDataTable(list);
    }

    /**
     * 导出菜品列表
     */
    @PreAuthorize("@ss.hasPermi('system:dish:export')")
    @Log(title = "菜品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TDish tDish){
        List<TDish> list = tDishService.selectTDishList(tDish);
        ExcelUtil<TDish> util = new ExcelUtil<TDish>(TDish.class);
        util.exportExcel(response, list, "菜品数据");
    }

    /**
     * 获取菜品详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:dish:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tDishService.selectTDishById(id));
    }

    /**
     * 新增菜品
     */
    @PreAuthorize("@ss.hasPermi('system:dish:add')")
    @Log(title = "菜品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDish tDish)
    {
        return toAjax(tDishService.insertTDish(tDish));
    }

    /**
     * 修改菜品
     */
    @PreAuthorize("@ss.hasPermi('system:dish:edit')")
    @Log(title = "菜品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDish tDish)
    {
        return toAjax(tDishService.updateTDish(tDish));
    }

    /**
     * 删除菜品
     */
    @PreAuthorize("@ss.hasPermi('system:dish:remove')")
    @Log(title = "菜品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tDishService.deleteTDishByIds(ids));
    }


    /**
     * wx小程序查询菜品列表
     */
    @GetMapping("/dishList")
    public AjaxResult dishList() {
        //在字典中获取菜品分类列表
        List<SysDictData> categoriesList = dictTypeService.getMenuCategories();

        List<CategorieVo> res = new ArrayList<>();
        //循环输出分类中的菜品列表
        for (int i = 0; i < categoriesList.size(); i++) {

            SysDictData categoriesData = categoriesList.get(i);  //获取第i个分类
            String dictValue = categoriesData.getDictValue();  //获取i分类中的字典值
            String dictLabel = categoriesData.getDictLabel();  //获取i分类中的字典名称 如： 主食 饮料 青菜 肉菜
            List<TDish> dishList = tDishService.selectTDishListByCategories(dictValue);  //根据字典值查询第i个分类的菜品列表

            CategorieVo categorieVo = new CategorieVo();
            categorieVo.setName(dictLabel);
            categorieVo.setFoods(dishList);
            res.add(categorieVo);
        }
        return AjaxResult.success(res);
    }

    /**
     * 微信小程序获取菜品详细信息
     */
    @GetMapping(value = "/getDishDetailById")
    public AjaxResult getDishDetail(@PathVariable("id") String id)
    {
        return success(tDishService.selectTDishById(id));
    }

}
